import { Component, HostListener, ViewContainerRef, ElementRef, Renderer,Renderer2 } from '@angular/core';
import { ActivatedRoute, Router, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot } from '@angular/router';
import { TranslateService } from 'ng2-translate';
import { userService } from "./user/share/user.service";
import { User } from "./user/share/user.model";
import { UserLoginService } from "./user/user-login/shared/user-login.service";
import {AppWindow} from "./utils/loadJScript";
//import { LoadingService } from "./common/loading/loading.service";
//import { LoadingComponent } from "./common/loading/loading.component";
// import Immutable = require('immutable');

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  //providers: [LoadingService]
})
export class AppComponent {
  public user: User;
  isUser: boolean = false;
  public isShort: Boolean = true;
  private globalClickCallbackFn: Function;
  private loginSuccessCallbackFn: Function;
  private mycontainerCallbackFn: Function;
  public chooseEt: boolean;
  isAdmin: boolean = false;
  constructor(
    public elementRef: ElementRef,
    public renderer: Renderer,
    private renderer2: Renderer2,
    public router: Router,
    public translate: TranslateService,
    public activatedRoute: ActivatedRoute,
    public userService: userService,
    public userLoginService: UserLoginService,
    public appWindow :AppWindow
  ) {
    ///#00f;
    // loadService.defaultViewContainerRef = viewContainer;
  }
 
  ngOnInit() {
    this.appWindow.createScript("http://pv.sohu.com/cityjson?ie=utf-8",this.renderer2,this.elementRef,()=>{
       console.log(returnCitySN);
      alert("ip:"+returnCitySN.cip+"城市："+returnCitySN.cname)
    });
    // this.loadService.show(LoadingComponent);
    this.globalClickCallbackFn = this.renderer.listen(this.elementRef.nativeElement, 'click', (event: any) => {
      console.log("全局监听点击事件>" + event);
    });
    //从缓存中获取用户信息
    this.user = JSON.parse(localStorage.getItem("user"));
    if (this.user) {
      this.userService.setUser(this.user);
      this.isUser = this.userService.isLogin;
      //判断用户是否为admin用户
      if (this.user.id == 1) {
        this.isAdmin = true;
      }
      console.log("当前用户");
      console.log(this.user);
    }

    console.log("路由状态：");
    console.log(this.router.routerState);
    this.userService.getUser().subscribe(
      data => {
        if (data.id != null) {
          this.user = data;
          let activatedRouteSnapshot: ActivatedRouteSnapshot = this.activatedRoute.snapshot;
          let routerState: RouterState = this.router.routerState;
          let routerStateSnapshot: RouterStateSnapshot = routerState.snapshot;
          console.log(this.user);
          this.isUser = true;
          localStorage.setItem("user", JSON.stringify(data));
          if (this.user.id == 1) {
            this.isAdmin = true;
          }
          //如果是从/login这个URL进行的登录，跳转到首页，否则什么都不做
          if (routerStateSnapshot.url.indexOf("user/login") != -1) {
            this.router.navigateByUrl("");
          }
        }
      }
    );
  }


  toggle(button: any) {
    // 判断当前状态
    if (this.isShort) {
      this.isShort = false;
    } else {
      this.isShort = true;
    }
  }

  closeTog() {
    //判断当前状态，如果是展开就关闭
    if (this.isShort == false) {
      this.isShort = true;
    }
  }
  logout() {
    localStorage.removeItem("user");
    this.isUser = false;
    this.isAdmin = false;
    this.userService.setUser(Object.assign({}));
    let activatedRouteSnapshot: ActivatedRouteSnapshot = this.activatedRoute.snapshot;
    let routerState: RouterState = this.router.routerState;
    let routerStateSnapshot: RouterStateSnapshot = routerState.snapshot;
    //如果是从/detail forgetpassword collection这个URL进行的登录，跳转到首页，否则什么都不做
    if (routerStateSnapshot.url.indexOf("user/*") != -1
      //  ||routerStateSnapshot.url.indexOf("user/collection")!=-1
      //  ||routerStateSnapshot.url.indexOf("user/forgetpassword")!=-1
      || routerStateSnapshot.url.indexOf("article/write") != -1
    ) {
      this.router.navigateByUrl("");
    }
  }

  closeMadel(isClose: boolean) {
    console.log("关闭弹框");
    console.log(isClose);
  }
  ngOnDestroy(){
    //销毁前调用
  }
}
